package com.zwps.biz.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwps.biz.api.model.dto.dsmspgpzwl.*;
import com.zwps.biz.api.model.vo.dsmspgpzwl.*;
import com.zwps.biz.dal.db.dao.PubPointAllDAO;
import com.zwps.biz.dal.db.dao.StPptnRDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.dal.db.dao.DsmSpgPzwlDAO;
import com.zwps.biz.dal.db.object.DsmSpgPzwlDO;
import com.zwps.biz.domain.service.DsmSpgPzwlService;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;

@Service
public class DsmSpgPzwlServiceImpl extends ServiceImpl<DsmSpgPzwlDAO, DsmSpgPzwlDO> implements DsmSpgPzwlService {
    @Autowired
    private DsmSpgPzwlDAO dsmSpgPzwlDAO;

    @Autowired
    private PubPointAllDAO pubPointAllDAO;

    @Autowired
    private StPptnRDAO stPptnRDAO;

    @Override
    public List<DsmSpgPzwVO> getPzwl(DsmSpgPzwDTO dspdto) {

        return dsmSpgPzwlDAO.getPzwl(dspdto);

    }

    @Override
    public PaginatedResult<DsmSpgPzwDateVO> getData(DsmSpgPzwDateDTO dspdto) {
        dspdto.setMode("0".equals(dspdto.getMode()) ? "" : "1".equals(dspdto.getMode()) ? "_manual" : dspdto.getMode());
        dspdto.setPage(null == dspdto.getPage() || dspdto.getPage().equals("") || dspdto.getPage() <= 0 ? 1 : dspdto.getPage());
        dspdto.setPageSize(null == dspdto.getPageSize() || dspdto.getPageSize().equals("") || dspdto.getPageSize() <= 0 ? 15 : dspdto.getPageSize());
        List<String> stringList=new ArrayList<>();
        if (dspdto.getMpcd()!=null && !dspdto.getMpcd().equals("")){
            stringList.add(dspdto.getMpcd());
        }else if (dspdto.getSccd()==null || dspdto.getSccd().equals("")){
            stringList= pubPointAllDAO.querryOtherSection(dspdto.getHycncd());
            if (stringList.size()==0) {
                return new PaginatedResult<DsmSpgPzwDateVO>();
            }
        }else {
            List<DsmSectionVO> sectionVOS = pubPointAllDAO.querrySection(dspdto.getSccd());
            if (sectionVOS.size()==0) {
                return new PaginatedResult<DsmSpgPzwDateVO>();
            }
            for (DsmSectionVO section : sectionVOS) {
                stringList.addAll(
                        Arrays.stream(section.getPtcds().split(","))
                                .map(String::trim)
                                .collect(Collectors.toList())
                );
            }
        }
        Map<String, Object> params = new HashMap<>();
        params.put("stringList", stringList);
        params.put("startTime", dspdto.getStartTime());
        params.put("endDTime", dspdto.getEndTime());
        params.put("sccd", dspdto.getSccd());
        params.put("mode", dspdto.getMode());
        params.put("mpcd", dspdto.getMpcd());

        Page<DsmSpgPzwDateVO> page = new Page<>(dspdto.getPage(), dspdto.getPageSize());
        IPage<Map<String, Object>> data = dsmSpgPzwlDAO.getData(page, params);
        List<DsmSpgPzwDateVO> dsmSpgPzwDateVOS = processQueryResults(data.getRecords());
        Map<String, Object> dynamicColumns = dsmSpgPzwDateVOS.get(0).getDynamicColumns();

        if (new BigDecimal(dynamicColumns.get("库水位").toString()).compareTo(BigDecimal.ZERO) == 0) {
            Date mstm = dsmSpgPzwDateVOS.get(0).getMstm();
            if (mstm != null) {
                String mstms = mstm.toString();
                dynamicColumns.put("库水位", stPptnRDAO.getRsvr(mstms));
            }

        }


        return new PaginatedResult<DsmSpgPzwDateVO>(dsmSpgPzwDateVOS, data.getTotal(), data.getSize(), data.getCurrent(), data.getPages());
    }


    public List<DsmSpgPzwDateVO> processQueryResults(List<Map<String, Object>> queryResults) {
        List<DsmSpgPzwDateVO> results = new ArrayList<>();
        for (Map<String, Object> row : queryResults) {
            DsmSpgPzwDateVO queryResult = new DsmSpgPzwDateVO();
            queryResult.setMstm((Date) row.get("mstm"));
            queryResult.setDrp((BigDecimal) row.get("drp"));
            queryResult.setCkflz((BigDecimal) row.get("ckflz"));
            queryResult.setDamel((BigDecimal) row.get("damel"));

            Map<String, Object> dynamicColumns = new HashMap<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                String columnName = entry.getKey();
                Object columnValue = entry.getValue();

                if (!Arrays.asList("mstm", "drp", "ckflz", "damel").contains(columnName)) {
                    dynamicColumns.put(columnName, columnValue);
                }
            }
            queryResult.setDynamicColumns(dynamicColumns);
            results.add(queryResult);
        }
        return results;
    }


    @Override
    public DsmMaxMinVO getMaxMin(DsmMaxMinDTO dspdto) {
        dspdto.setMode("0".equals(dspdto.getMode()) ? "" : "1".equals(dspdto.getMode()) ? "_manual" : dspdto.getMode());
        List<String> stringList=new ArrayList<>();
        if (dspdto.getMpcd()!=null && !dspdto.getMpcd().equals("")){
            stringList.add(dspdto.getMpcd());
        }else if (dspdto.getSccd()==null || dspdto.getSccd().equals("")){
            stringList= pubPointAllDAO.querryOtherSection(dspdto.getHycncd());
            if (stringList.size()==0) {
                return new DsmMaxMinVO();
            }
        }else {
            List<DsmSectionVO> sectionVOS = pubPointAllDAO.querrySection(dspdto.getSccd());

            for (DsmSectionVO section : sectionVOS) {
                stringList.addAll(
                        Arrays.stream(section.getPtcds().split(","))
                                .map(String::trim)
                                .collect(Collectors.toList())
                );
            }
        }
        stringList.add("库水位");
        Map<String, Object> params = new HashMap<>();
        params.put("mpcdList", stringList);
        params.put("startTime", dspdto.getStartTime());
        params.put("endTime", dspdto.getEndTime());
        List<MaxMinManometryVO> data = dsmSpgPzwlDAO.getMaxMin(params);
        DsmMaxMinVO vo = new DsmMaxMinVO();
        vo.setManometry(data);
        List<MaxMinDrpVO> drpVO = stPptnRDAO.getMaxMinDrp(params);
        vo.setMdrp(drpVO);

        return vo;

    }

    @Override
    public IPage<DsmHistoryDateVo> gethistoryData(DsmHistoryDateDTO historyDateDTO) {

        IPage<DsmHistoryDateVo> page = new Page<>(historyDateDTO.getPageNum(), historyDateDTO.getPageSize());
      return   dsmSpgPzwlDAO.gethistoryData(page,historyDateDTO.getMpcd(),historyDateDTO.getStartTime(),historyDateDTO.getEndTime(),historyDateDTO.getType());
    }

    @Override
    public  Map<String,Object> getPzwlList() {
        List<DsmSpgPzwVO> pzwlList = dsmSpgPzwlDAO.getPzwlList();
        Map<String,Object> map=new HashMap<>();
        for (DsmSpgPzwVO dsmSpgPzwVO : pzwlList) {
            map.put(dsmSpgPzwVO.getPtcd(),dsmSpgPzwVO.getPztbwl());
        }
        return map;
    }
}
